package com.base.vistter.iframe.inverter.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.InverterDevices;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.model.DevicesElectricityOverview;
import com.base.vistter.iframe.model.StationElectricityOverview;
import com.base.vistter.iframe.service.InverterDevicesService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/devices")
public class InverterDevicesController {

    @Resource(name = "inverterDevicesService")
    private InverterDevicesService inverterDevicesService;

    @GetMapping
    public Result<Page<InverterDevices>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum,Integer stationId,  String no, String code) throws IframeException {
        Page<InverterDevices> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<InverterDevices> lambdaQueryWrapper = new LambdaQueryWrapper<InverterDevices>();
        lambdaQueryWrapper.eq(InverterDevices::getInverterStationId, stationId);
        if (StringUtils.isNotEmpty(no)) {
            lambdaQueryWrapper.like(InverterDevices::getNo, no);
        }
        if(StringUtils.isNotEmpty(code)){
            lambdaQueryWrapper.like(InverterDevices::getCode, code);
        }
        return Result.ok(inverterDevicesService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<InverterDevices> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterDevicesService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody InverterDevices inverterDevices) throws IframeException {
        return Result.ok(inverterDevicesService.save(inverterDevices));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody InverterDevices inverterDevices) throws IframeException {
        return Result.ok(inverterDevicesService.update(inverterDevices));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterDevicesService.delete(id));
    }

    @GetMapping("/status/count")
    public Result<List<Map<String, Object>>> countStatus(Integer stationId) throws IframeException {
        return Result.ok(inverterDevicesService.countStatus(stationId));
    }

    @GetMapping("/search")
    public Result<InverterDevices> search(String no) throws IframeException {
        LambdaQueryWrapper<InverterDevices> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(InverterDevices::getNo, no).last("LIMIT 1");
        return Result.ok(inverterDevicesService.selectOne(lambdaQueryWrapper));
    }

    @GetMapping("/electricity")
    public Result<DevicesElectricityOverview> getStationElectricityOverview(Integer devicesId) throws IframeException {
        return Result.ok(inverterDevicesService.getDevicesElectricity(devicesId));
    }

}
